package com.itheima.behaviour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.behaviour.dto.LikesBehaviourDto;
import com.itheima.behaviour.mapper.ApBehaviorEntryMapper;
import com.itheima.behaviour.mapper.ApLikesBehaviorMapper;
import com.itheima.behaviour.pojo.ApBehaviorEntry;
import com.itheima.behaviour.pojo.ApLikesBehavior;
import com.itheima.behaviour.service.ApLikesBehaviorService;
import com.itheima.common.constants.SystemConstants;
import com.itheima.common.enums.HttpCodeEnum;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.util.RequestContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @description <p>APP点赞行为 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.behaviour.service.impl
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    /**
     * APP点赞与取消
     *
     * @param likesBehaviourDto
     */
    @Override
    public void likeBehavior(LikesBehaviourDto likesBehaviourDto) {
        //1. 参数校验
        if(likesBehaviourDto == null
                || likesBehaviourDto.getArticleId() == null
                || likesBehaviourDto.getType() > 2
                || likesBehaviourDto.getType() < 0
                || likesBehaviourDto.getOperation() < 0
                || likesBehaviourDto.getOperation() > 1){
            throw new LeadNewsException(HttpCodeEnum.PARAM_INVALID);
        }
        //2. 判断操作实体是匿名还是登录用户
        int loginUserId = Integer.parseInt(RequestContextUtil.getLoginUserId());
        // 假设实体为当前登录用户id
        int entryId = loginUserId;
        // 假设实体类型为用户
        int type = SystemConstants.TYPE_USER;
        if(SystemConstants.ANONYMOUS_USER_ID == loginUserId){
            //如果是匿名，则实体类型为备
            type = SystemConstants.TYPE_E;
            //那么相应的，实体Id就为设备的id
            entryId = likesBehaviourDto.getEquipmentId();
        }
        ApBehaviorEntry entry = apBehaviorEntryMapper.findByEntryIdAndType(entryId, type);
        if(null != entry) {
            //判断记录是否存在，如果存在
            LambdaQueryWrapper<ApLikesBehavior> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ApLikesBehavior::getArticleId, likesBehaviourDto.getArticleId())
                    .eq(ApLikesBehavior::getEntryId, entry.getId())
                    .eq(ApLikesBehavior::getType,likesBehaviourDto.getType());//类型为文章
            ApLikesBehavior likesBehavior = getOne(lqw);
            if(null == likesBehavior){
                // 不存在则添加
                ApLikesBehavior pojo = new ApLikesBehavior();
                pojo.setArticleId(likesBehaviourDto.getArticleId());
                pojo.setEntryId(entry.getId());
                pojo.setType(likesBehaviourDto.getType());//文章
                pojo.setOperation(likesBehaviourDto.getOperation());
                pojo.setCreatedTime(LocalDateTime.now());
                save(pojo);
            }else{
                //获取已存在的操作类型
                Integer dbOperation = likesBehavior.getOperation();
                if(dbOperation.intValue() != likesBehaviourDto.getOperation().intValue()){
                    //如果操作类型不同，则更新
                    ApLikesBehavior updatePojo = new ApLikesBehavior();
                    updatePojo.setOperation(likesBehaviourDto.getOperation());
                    updatePojo.setId(likesBehavior.getId());
                    updateById(updatePojo);
                }
            }
        }
    }


    /**
     * 通过文章Id与行为实体Id 查询是否点赞
     *
     * @param articleId
     * @param entryId
     * @return
     */
    @Override
    public ApLikesBehavior findByArticleIdAndEntryId(Long articleId, Integer entryId) {
        LambdaQueryWrapper<ApLikesBehavior> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ApLikesBehavior::getArticleId, articleId)
                .eq(ApLikesBehavior::getEntryId, entryId);
        return getOne(lqw);
    }
}
